<?php
class Admin_Model_RoleMapper {

	protected $_dbTable;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Admin_Model_DbTable_Role');
		}
		return $this->_dbTable;
	}


	public function fetchAll($query = null,$option='view') {
		if ($query == null) {
			$resultSet = $this->getDbTable()->fetchAll();
		} else {
			$resultSet = $this->getDbTable()->fetchAll($query);
		}
		return $this->processResultSet($resultSet->toArray(),$option);
	}

	/*
	 *processResultSet
	 */
	private function processResultSet($resultSet,$option='view') {
		$entries = array();
		if($option=='view'){
			foreach ($resultSet as $row) {
				$entry = new Admin_Model_TableColumn_Role($row);
				$entries[] = $entry;
			}
			return $entries;
		}else{
			foreach ($resultSet as $row) {
				$entries[] = $row;
			}
			return $entries;
		}
	}

	public function delete($id) {
		$table = $this->getDbTable();
		$table->delete("id = $id");
	}
}